/**
 * AV1 codec support detection utility
 */

export interface AV1SupportResult {
  encoding: boolean;
  decoding: boolean;
  hardwareAcceleration: boolean;
  supportedProfiles: string[];
}

/**
 * Common AV1 codec configurations
 */
const AV1_CODECS = [
  'av01.0.00M.08',      // Main Profile, Level 2.0
  'av01.0.04M.08',      // Main Profile, Level 3.0
  'av01.0.05M.08',      // Main Profile, Level 3.1
  'av01.0.08M.08',      // Main Profile, Level 4.0
  'av01.0.09M.08',      // Main Profile, Level 4.1
];

/**
 * Check AV1 encoding support
 */
export async function checkAV1EncodingSupport(
  width = 1280,
  height = 720,
  bitrate = 2_000_000,
  framerate = 30
): Promise<Array<{ codec: string; hardwareAcceleration?: string }>> {
  if (!('VideoEncoder' in window)) {
    return [];
  }

  const supportedConfigs = [];

  for (const codec of AV1_CODECS) {
    try {
      const config = {
        codec,
        width,
        height,
        bitrate,
        framerate,
        hardwareAcceleration: 'prefer-hardware' as const,
      };

      const result = await VideoEncoder.isConfigSupported(config);
      console.log('AV1 encoding support check for codec', codec, ':', result);
      if (result.supported) {
        supportedConfigs.push({
          codec,
          hardwareAcceleration: result.config?.hardwareAcceleration,
        });
      }
    } catch (e) {
      continue;
    }
  }

  return supportedConfigs;
}

/**
 * Check AV1 decoding support
 */
export async function checkAV1DecodingSupport(): Promise<{ supported: boolean; codec?: string }> {
  if (!('VideoDecoder' in window)) {
    return { supported: false };
  }

  for (const codec of AV1_CODECS) {
    try {
      const config = {
        codec,
      };

      const result = await VideoDecoder.isConfigSupported(config);
      
      if (result.supported) {
        return {
          supported: true,
          codec,
        };
      }
    } catch (e) {
      continue;
    }
  }

  return { supported: false };
}

/**
 * Simple boolean check - whether AV1 encoding is supported
 */
export async function isAV1Supported(): Promise<boolean> {
  const result = await checkAV1EncodingSupport();
  return result.supported;
}

/**
 * Get recommended AV1 codec string
 */
export async function getRecommendedAV1Codec(): Promise<string | null> {
  const result = await checkAV1EncodingSupport();
  return result.codec || null;
}

/**
 * Comprehensive AV1 support detection
 */
export async function checkAV1Support(
  encodingConfig?: {
    width?: number;
    height?: number;
    bitrate?: number;
    framerate?: number;
  }
): Promise<AV1SupportResult> {
  const [encodingResult, decodingResult] = await Promise.all([
    checkAV1EncodingSupport(
      encodingConfig?.width,
      encodingConfig?.height,
      encodingConfig?.bitrate,
      encodingConfig?.framerate
    ),
    checkAV1DecodingSupport(),
  ]);

  const supportedProfiles: string[] = [];
  
  for (const codec of AV1_CODECS) {
    try {
      const encResult = await VideoEncoder.isConfigSupported({
        codec,
        width: 1280,
        height: 720,
        bitrate: 2_000_000,
        framerate: 30,
      });
      
      if (encResult.supported) {
        supportedProfiles.push(codec);
      }
    } catch (e) {
      // Ignore errors
    }
  }

  return {
    encoding: encodingResult.supported,
    decoding: decodingResult.supported,
    hardwareAcceleration: encodingResult.hardwareAcceleration === 'prefer-hardware',
    supportedProfiles,
  };
}

/**
 * Get all supported AV1 codec configurations
 */
export async function getSupportedAV1Configs(
  width = 1280,
  height = 720,
  bitrate = 2_000_000,
  framerate = 30
): Promise<Array<{
  codec: string;
  width: number;
  height: number;
  bitrate: number;
  framerate: number;
  hardwareAcceleration?: string;
  supported: boolean;
}>> {
  if (!('VideoEncoder' in window)) {
    return [];
  }

  const configs = [];

  for (const codec of AV1_CODECS) {
    try {
      const config = {
        codec,
        width,
        height,
        bitrate,
        framerate,
        hardwareAcceleration: 'prefer-hardware' as const,
      };

      const result = await VideoEncoder.isConfigSupported(config);
      
      configs.push({
        codec,
        width,
        height,
        bitrate,
        framerate,
        hardwareAcceleration: result.config?.hardwareAcceleration,
        supported: result.supported,
      });
    } catch (e) {
      configs.push({
        codec,
        width,
        height,
        bitrate,
        framerate,
        supported: false,
      });
    }
  }

  return configs;
}

export default {
  checkAV1Support,
  checkAV1EncodingSupport,
  checkAV1DecodingSupport,
  isAV1Supported,
  getRecommendedAV1Codec,
  getSupportedAV1Configs,
};